APM.ConfigIprepairController = Ember.Controller.extend({
	init : function() {
		this._super();
		this.initConfData();
	},
	
	form: {
		startIp:"",
		endIp:"",
		memo:"",
		areaCode: "00",
		IpareaIndex:"",
		isDedicate: false,
		isUpdate: false,
		confType:"6",
		startIp1:"",
		memo1:"",
		areaCode1: "00"
	},
	pageIndex: 0, // 默认加载第一页
	pageSize: 10,
	initConfData: function(){
		var ctrl = this;
		var form = this.get("form");
		ctrl.store.ajax("GET", "configs/systemConfig",{
			confType:form.confType,
			configItem:"map"
		}).then(function(response){
			var configobj=response.configData;
			if(configobj){
				if(configobj.map_internal_enabled=="1"){
					ctrl.set("form.isDedicate",true);
				}else{
					ctrl.set("form.isDedicate",false);
				}
				if(configobj.map_internal_auto_sync =="1"){
					 ctrl.set("form.isUpdate",true);
				}else{
					 ctrl.set("form.isUpdate",false);
				}
			}	
		});
		var pageIndex = ctrl.get("pageIndex") || 0;
		this.store.ajax("GET", "configs/iprepair",{
			startIp:form.startIp,
			areaCode:form.areaCode,
			memo:form.memo,
			pageIndex: pageIndex,
			pageSize: 10
		}).then(function(response){
			var IpAreas=response.IpAreas;
			if(IpAreas){	 
				if(IpAreas[0]){
					ctrl.set("pageIndex", response.pageIndex);
					ctrl.set("pageSize", response.pageSize);
					ctrl.set("totalRows", response.total);
				}else{
					ctrl.set("pageIndex", 0);
					ctrl.set("pageSize", 10);
					ctrl.set("totalRows", 0);
				}
				ctrl.set("ipArealists",Ember.A(IpAreas));	
			}
		});
		
	},
	
	//IP地理位置库列表存放数组
	// ipArealists : Ember.computed(function(key, value){
	// 	if(arguments.length == 1){
	// 		return Ember.A();
	// 	}else{
	// 		return value;
	// 	}
	// }),
	ipArealists: Ember.A(),
	gridColumns: [
		{
			headerText: "IP",
			dataKey:"id",
			width: 300,
			allowSort: true,
			ensurePxWidth: true,
		},
		{
			headerText: "地址",
			dataKey:"areaName",
			width: 300,
			ensurePxWidth: true
		},
		{
			headerText: "备注",
			dataKey:"memo",
			allowSort: true,
			ensurePxWidth: true,
			cellFormatter: function (args, colume) {
				
			}
		},
		{	
			headerText: "操作",
			dataKey:"areaCode",
			width: 200,
			allowSort: false,
			ensurePxWidth: true,
			cellFormatter: function (args, colume) {
				var ctrl = this.get("controller");
				var $editBtn = $('<button class="u-btn-blue ml10" title="编辑">编辑</button>');
				var $delBtn = $('<button class="u-btn-danger ml10" title="删除">删除</button>');
				$editBtn.on("click",function(){
					ctrl.updOpenDialog(args.row.data);
					//ctrl.send("updOpenDialog",args.row.data);
				});
				$delBtn.on("click",function(){
					ctrl.delIpArea(args.row.data.id);
				});
				args.$container.append($editBtn).append($delBtn);
				return true; 	
			}
		}
	],
	
	checkFormValid: function(){
		var form = this.get("form");
		this.set("formTip", null);
		if(form.startIp){
			var reg1=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!reg1.test(form.startIp)){  
			   this.set("formTip", "对不起，您输入的开始IP地址非法，请重新输入!");
			   return false;
			}  
		}
		if(form.endIp){
			var reg1=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!reg1.test(form.endIp)){  
				this.set("formTip", "对不起，您输入的结束IP地址非法，请重新输入!");
				return false;
			}
		}
		
		if(form.startIp && form.endIp){
			var startIp1=form.startIp;
			var endIp1=form.endIp;
			var startIpint=parseInt(startIp1.replace(/\d+\.?/ig,function(a){
								a = parseInt(a); 
								return (a > 15 ? "" : "0") + a.toString(16);
							}),16);
			var endIpint=parseInt(endIp1.replace(/\d+\.?/ig,function(b){
				b = parseInt(b); 
				return (b > 15 ? "" : "0") + b.toString(16);
			}),16);
			if(startIpint>endIpint){
				this.set("formTip", "开始IP地址不应大于结束IP地址!");
				return false;
			}
			if(startIpint == endIpint){
				this.set("formTip", "开始IP地址与结束IP地址相同!");
				return false;
			}
		}
		
		this.set("formTip", null);
		return true;
	},
	updOpenDialog: function(obj){
			var ctrl=this;
			ctrl.set("formTip", null);
			ctrl.set("saveResult", null);
			ctrl.set("citylists", Ember.A());
			ctrl.set("distlists", Ember.A());
			ctrl.set("form.areaCode","00");
			var form=ctrl.get("form");
			if(obj){
				var areaObj=obj.area;
				this.set("form.startIp",obj.startIp);
				this.set("form.endIp",obj.endIp);
				this.set("form.memo",obj.memo);
				this.set("form.id",obj.id);
				this.set("form.areaCode",obj.areaCode);
				// 设置地理位置
				$("#iprepair_upd .select-content").each(function(index){
					$(this).text(areaObj[index].name);
				});
			}
			var dialog = Broada.getView("iprepair_upd");
			Broada.getView("iprepair_upd").openDialog();
      },
	delIpArea: function(areaIndex){
			var ctrl = this;
			var form = this.get("form");
			ctrl.set("saveResult", null);
			
			confirmDialog("确定要删除该数据吗？")
			.then(function(){
				ctrl.store.ajax("DELETE", "configs/iprepair", {
					IpareaIndex:areaIndex
				}).then(function(response){
					if(response.status=="success"){
						//todo: 保存成功逻辑
						alertTip("删除成功！");
					}else{
						alertTip(response.message);
					}
					ctrl.set("form.startIp",null);
					ctrl.set("form.endIp",null);
					ctrl.set("form.areaCode", "00");
					ctrl.set("form.memo",null);
					ctrl.send("queryAreaAll");
				});
			});
		},
	queryPageArea:function(){
		var ctrl = this;
		ctrl.store.ajax("GET", "configs/iprepair",{
			startIp:null,
			areaCode:"",
			memo:null,
			pageIndex: ctrl.get("pageIndex"),
			pageSize: ctrl.get("pageSize")
		}).then(function(response){
 			var IpAreas=response.IpAreas;
 			if(IpAreas){
				if(IpAreas.length){
					ctrl.set("pageIndex",response.pageIndex);
					ctrl.set("pageSize",response.pageSize);
					ctrl.set("totalRows",response.total);
				}else{
					ctrl.set("pageIndex", 0);
					ctrl.set("pageSize", 2);
					ctrl.set("totalRows", 0);
				}
 			}
 			ctrl.set("ipArealists",Ember.A(IpAreas));	
		});
	},
	actions: {
		reset : function() {
			var ctrl=this;
			ctrl.set("formTip", null);
			ctrl.set("saveResult", null);
			ctrl.set("form.startIp1",null);
			ctrl.set("form.memo1",null);
		},
		
		//添加对话框操作打开方法
		openDialog: function(){
			var ctrl=this;
			ctrl.set("formTip", null);
			ctrl.set("saveResult", null);
			var form=ctrl.get("form");
			ctrl.set("form.areaCode","00");
			ctrl.set("form.startIp",null);
			ctrl.set("form.endIp",null);
			ctrl.set("form.memo",null);
			ctrl.set("citylists", Ember.A());
			ctrl.set("distlists", Ember.A());
			var addrInit = ["中国","省份","市","区/县"];
			$("#iprepair_add .select-content").each(function(index){
				$(this).text(addrInit[index]);
			});
			var dialog = Broada.getView("iprepair_add");
			Broada.getView("iprepair_add").openDialog();
       },

       //ip地理位置库导入功能对话框开启
       leadInDialog:function(){
       		this.set("formTip", null);
			this.set("saveResult", null);
			this.set("showImg",false);
       		var dialog = Broada.getView("config-iprepair-leadIn");
			Broada.getView("config-iprepair-leadIn").openDialog();
       },

        //内置库导入功能对话框开启
       innerLibDialog:function(){
       		this.set("formTip", null);
			this.set("saveResult", null);
			this.set("showImg",false);
			Broada.getView("iprepair_inner_Lib").openDialog();
       },

       leadIn:function(){
       		var ctrl = this;
       		
       		if($("#FileText").val() == ""){
       			this.set("formTip", "请选择文件！");
       			return;
       		}
       		this.set("showImg",true);
       		APM.iframeCallback = function(status,num,message) {
				if(status){
					if(status==1){
						ctrl.send("queryAreaAll");
						alertTip("文件成功导入"+ num +"条有效数据！");
					}
					if(status==2){
						alertTip('文件批量保存失败！');
					}
					if(status==3){
						alertTip('导入数据为空！');
					}
					if(status==4){
						alertTip('上传失败,文件应为csv格式！');
					}
					if(status==5){
						alertTip('上传失败,文件尺寸过大!');
					}
					if(status==6){
						alertTip('上传失败,文件格式受限制且尺寸过大!');
					}
					if(status==7){
						alertTip('没有选择上传文件！');
					}
					if(status==8){
						alertTip('上传失败,'+message);
					}
				}
				ctrl.set("showImg",false);
				ctrl.send("closeDialog","3");
				APM.iframeCallback = null;
			};
			
			var form1 = $('#leadForm');
			form1[0].method="POST";
			form1[0].action="../front/rest/apm/configs/iprepair/ipFileupload?token="+encodeURIComponent($.cookie("token"));
			form1[0].submit();
       },
       
       //修改对话框打开方法
		updOpenDialog: function(obj){
			var ctrl=this;
			ctrl.set("formTip", null);
			ctrl.set("saveResult", null);
			ctrl.set("citylists", Ember.A());
			ctrl.set("distlists", Ember.A());
			ctrl.set("form.areaCode","00");
			var form=ctrl.get("form");
			if(obj){
				var areaObj=obj.area;
				this.set("form.startIp",obj.startIp);
				this.set("form.endIp",obj.endIp);
				this.set("form.memo",obj.memo);
				this.set("form.id",obj.id);
				this.set("form.areaCode",obj.areaCode);
			}
			var dialog = Broada.getView("iprepair_upd");
			Broada.getView("iprepair_upd").openDialog();
      },
       
       closeDialog: function(diaIndex){
    	   if(diaIndex=="1"){
    		   Broada.getView("iprepair_add").closeDialog();
    		}
    	   
    	   if(diaIndex=="2"){
    		   Broada.getView("iprepair_upd").closeDialog();
    		}

    		if(diaIndex=="3"){
    		   Broada.getView("config-iprepair-leadIn").closeDialog();
    		}
    		if(diaIndex=="4"){
    		   Broada.getView("iprepair_inner_Lib").closeDialog();
    		}
       },
       
     //内置库立即更新
		updateNow: function(){
				var ctrl = this;
				ctrl.store.ajax("PUT", "configs/iprepair/innerlib").then(function(response){
					if(response.status=="success"){
						//todo: 保存成功逻辑
						alertTip("更新成功！");
					}else{
						alertTip(response.message);
					}
				});
		},
			
		//内置库配置保存
		save: function(){
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("saveResult", null);
			ctrl.store.ajax("POST", "configs/systemConfig", {
				isDedicate: String(Number(form.isDedicate)),
				isUpdate:String(Number(form.isUpdate)),
				confType:form.confType
			}).then(function(response){
				if(response.status=="success"){
					//todo: 保存成功逻辑
					alertTip("保存成功！");
				}else{
					alertTip(response.message);
				}
			});
		},
       
       addIpArea: function(){
			if (!this.checkFormValid()) {
				return;
			}
			var ctrl = this;
			var form = this.get("form");
			var areaCode=ctrl.get("form.areaCode");

			this.store.ajax("POST", "configs/iprepair",{
				startIp:form.startIp,
				endIp:form.endIp,
				areaCode:areaCode,
				memo:form.memo
			}).then(function(response){
				if(response.status=="success"){
					//todo: 保存成功逻辑
					alertTip("添加成功！");
				}else{
					alertTip(response.message);
				}
				ctrl.set("citylists", Ember.A());
				ctrl.set("distlists", Ember.A());
				ctrl.set("form.startIp",null);
				ctrl.set("form.endIp",null);
				ctrl.set("form.areaCode","00");
				ctrl.set("form.memo",null);
				ctrl.send("queryAreaAll");
			});
			ctrl.send("closeDialog","1");
		},
		
		delIpArea: function(areaIndex){
			var ctrl = this;
			var form = this.get("form");
			ctrl.set("saveResult", null);
			
			confirmDialog("确定要删除该数据吗？")
			.then(function(){
				ctrl.store.ajax("DELETE", "configs/iprepair", {
					IpareaIndex:areaIndex
				}).then(function(response){
					if(response.status=="success"){
						//todo: 保存成功逻辑
						alertTip("删除成功！");
					}else{
						alertTip(response.message);
					}
					ctrl.set("form.startIp",null);
					ctrl.set("form.endIp",null);
					ctrl.set("form.areaCode", "00");
					ctrl.set("form.memo",null);
					ctrl.send("queryAreaAll");
				});
			});
		},
		
		updIpArea: function(obj){
			if (!this.checkFormValid()) {
				return;
			}
			var ctrl = this;
			var form = this.get("form");
			ctrl.set("saveResult", null);
			var areaCode=ctrl.get("form.areaCode");

			this.store.ajax("PUT", "configs/iprepair",{
				id:form.id,
				startIp:form.startIp,
				endIp:form.endIp,
				areaCode:areaCode,
				memo:form.memo
			}).then(function(response){
				if(response.status=="success"){
					//todo: 保存成功逻辑
					alertTip("更新成功！");
				}else{
					alertTip(response.message);
				}
				ctrl.set("form.startIp",null);
				ctrl.set("form.endIp",null);
				ctrl.set("form.areaCode", "00");
				ctrl.set("form.memo",null);
				ctrl.send("queryAreaAll");
			});
			ctrl.send("closeDialog","2");
		},
		
		//查询IP地理位置库列表
		queryIpArea:function(){
			var ctrl = this;
			var form = this.get("form");
			var areaCode=ctrl.get("form.areaCode1");
			
			if(form.startIp1){
			   	var reg1=/^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))|(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))|((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]))$/;
	   			if(!reg1.test(form.startIp1)){  
	   			   alert("对不起，您输入的查询IP地址有误，请重新输入!");
	   			   return false;
	   			}  
	   		}
			
			this.store.ajax("GET", "configs/iprepair",{
				startIp:form.startIp1,
				areaCode:areaCode,
				memo:form.memo1
			}).then(function(response){
	 			var IpAreas=response.IpAreas;
	 			if(IpAreas){
					ctrl.set("ipArealists",Ember.A(IpAreas));
	 			}	
 			});
		},

		moduledownload:function(){
			var ctrl = this;
			confirmDialog("确定要下载该模板文件吗？")
			.then(function(){
				ctrl.set("formTip", null);
				ctrl.set("saveResult",null);
				var url="/front/rest/apm/configs/downloadIPMod?token="+encodeURIComponent($.cookie("token"));;
				window.location.href =url;
			});
		},

		//内置库上传
		uploadInnerLib: function(){
			var ctrl = this;
			this.set("showImg",true);
			APM.iframeCallback = function(status) {

				if(status){
					if(status==1){
						alertTip('文件上传成功！');
					}
					if(status==2){
						alertTip('上传失败,文件应为zip格式！');
					}
					if(status==3){
						alertTip('上传失败,文件尺寸过大!');
					}
					if(status==4){
						alertTip('上传失败,文件格式受限制且尺寸过大!');
					}
					if(status==5){
						alertTip('没有选择上传文件！');
					}
					if(status==6){
						alertTip('上传失败,出现异常！');
					}
					delete APM.iframeCallback;
				}
				ctrl.set("showImg",false);
				ctrl.send("closeDialog","4");
			};
			var form1 = $('#innerLibForm');
			form1[0].method="POST";
			form1[0].action="../front/rest/apm/configs/iprepair/innerLibupload?token="+encodeURIComponent($.cookie("token"));
			form1[0].submit();
		},
		
		queryAreaAll:function(){
			var ctrl = this;
			var pageIndex = this.get("pageIndex") || 0;
			//return;
			ctrl.store.ajax("GET", "configs/iprepair",{
				startIp:null,
				areaCode:"",
				memo:null,
				pageIndex: pageIndex,
				pageSize: 10
			}).then(function(response){
	 			var IpAreas=response.IpAreas;
	 			if(IpAreas[0]){
	 				ctrl.set("ipArealists",Ember.A(IpAreas));
					ctrl.set("totalRows",response.total);
	 			}else{
	 				ctrl.set("pageIndex",0);
					ctrl.set("pageSize", 10);
					ctrl.set("totalRows",0);
	 			}

 			});
		},
		pageIndexChanged: function(e, data, view) {
			var pageNo = data.newPageIndex;
			this.set("pageIndex", pageNo);

			this.send("queryAreaAll");
		},

	}		
});